package com.ruoyi.user.service.impl;

import com.ruoyi.common.core.domain.model.UserBusinessInfo;
import com.ruoyi.common.core.service.UserDrugBusinessService;
import com.ruoyi.common.core.service.UserMemberBusinessService;
import com.ruoyi.user.domain.vo.UserFamilyVo;
import com.ruoyi.user.domain.vo.UserHelpGroupVo;
import com.ruoyi.user.domain.vo.UserRelationVo;
import com.ruoyi.user.domain.vo.relation.RelationLine;
import com.ruoyi.user.domain.vo.relation.RelationNode;
import com.ruoyi.user.domain.vo.relation.RelationWrapper;
import com.ruoyi.user.service.IUserFamilyService;
import com.ruoyi.user.service.IUserHelpGroupService;
import com.ruoyi.user.service.IUserRelationService;
import com.ruoyi.user.service.RelationService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: YangShiWei
 * Email: strongwalter2014@gmail.com
 * Datetime: 2023/4/9 17:44
 */
@Service
@AllArgsConstructor
public class RelationServiceImpl implements RelationService {

    private final UserMemberBusinessService userMemberBusinessService;
    private final UserDrugBusinessService userDrugBusinessService;
    private final IUserRelationService userRelationService;
    private final IUserFamilyService userFamilyService;
    private final IUserHelpGroupService userHelpGroupService;

    @Override
    public RelationWrapper buildRelation(Long rootId) {
        RelationWrapper wrapper = new RelationWrapper();
        UserBusinessInfo user = userDrugBusinessService.queryById(rootId);
        if (user == null) return wrapper;
        wrapper.setRootId(rootId);
        wrapper.getNodes().add(RelationNode.buildFromUserDrug(user));
        // 负责工作人员
        if (user.getFzr() != null) {
            UserBusinessInfo member = userMemberBusinessService.queryById(user.getFzr());
            if (member != null) {
                wrapper.getNodes().add(RelationNode.buildFromUserMember(member));
                wrapper.getLines().add(RelationLine.buildFromUserMember(rootId, member));
            }
        }
        List<UserFamilyVo> userFamilies = userFamilyService.queryByUserId(rootId);
        List<UserRelationVo> userRelationVos = userRelationService.queryByUserId(rootId);
        List<UserHelpGroupVo> userHelpGroupVos = userHelpGroupService.queryByUserId(rootId);
        List<RelationNode> nodeFamilies = userFamilies.stream().map(RelationNode::buildFromUserFamily).collect(Collectors.toList());
        List<RelationLine> lineFamilies = userFamilies.stream().map(d -> RelationLine.buildFromUserFamily(rootId, d)).collect(Collectors.toList());
        List<RelationNode> nodeRelations = userRelationVos.stream().map(RelationNode::buildFromUserRelation).collect(Collectors.toList());
        List<RelationLine> lineRelations = userRelationVos.stream().map(d -> RelationLine.buildFromUserRelation(rootId, d)).collect(Collectors.toList());
        List<RelationNode> nodeHelpGroups = userHelpGroupVos.stream().map(RelationNode::buildFromUserHelpGroup).collect(Collectors.toList());
        List<RelationLine> lineHelpGroups = userHelpGroupVos.stream().map(d -> RelationLine.buildFromUserHelpGroup(rootId, d)).collect(Collectors.toList());
        wrapper.getNodes().addAll(nodeFamilies);
        wrapper.getNodes().addAll(nodeRelations);
        wrapper.getNodes().addAll(nodeHelpGroups);
        wrapper.getLines().addAll(lineFamilies);
        wrapper.getLines().addAll(lineRelations);
        wrapper.getLines().addAll(lineHelpGroups);
        return wrapper;
    }
}
